import scrapy
from scrapy import Request


class Top250Spider(scrapy.Spider):
    name = "top250"
    allowed_domains = ["douban.com","doubanio.com"]
    # https://movie.douban.com/top250?start=0&filter=
    start_urls = ["https://movie.douban.com/top250?start=0&filter="]
    page = 1
    def parse(self, response):

        movie_list = response.xpath("//ol[@class='grid_view']/li")
        for movie in movie_list:

            item = {}
            item['id'] = movie.xpath("./div[@class='item']/div[@class='pic']/em/text()").extract_first()
            item['img_src'] = movie.xpath(".//img/@src").extract_first()
            director_info = movie.xpath(".//div[@class='bd']/p[1]/text()").extract()
            item['director'] = director_info[0].strip().replace(u'\xa0', u'')
            item['info'] = director_info[1].strip().replace(u'\xa0', u'')
            item['title'] = movie.xpath(".//span[@class='title']/text()").extract_first()
            item['score'] = movie.xpath(".//span[@class='rating_num']/text()").extract_first()
            item['comment_num'] = movie.xpath(".//div[@class='star']/span[4]/text()").extract_first()
            item['quote'] = movie.xpath(".//div[@class='bd']/p/span/text()").extract_first()
            item['type'] = 'info'

            yield item
            yield scrapy.Request(item['img_src'],callback=self.parse_img,cb_kwargs={'title':item['title']})
            # next_page_url = response.xpath("//span[@class='next']/a/@href").extract_first()
            # next_page_url = "https://movie.douban.com/top250" + next_page_url
            # 生成下一页地址
            self.page += 1
            if self.page <= 10:
                next_page_url = "https://movie.douban.com/top250?start={}&filter=".format((self.page - 1) * 25)
                yield Request(next_page_url,callback=self.parse)

    def parse_img(self,response,title):
        filename = title + '.jpg'
        yield {
            'type':'img',
            'filename':filename,
            'img':response.body
        }